
const db = require('../db/echarts.js')

const fs = require("fs");

const multiparty = require("multiparty");

const formidable = require("formidable")

const path =require("path")

let canvasId = 0

exports.saveCanvas = (req,res)=>{
  console.log('saveCanvas',req.body)
  const req_body_name = req.body.fileName
  const req_body_img = req.body.fileUrl

  var dataBuffer = Buffer.from(req_body_img, 'base64');
  let imgPath = path.join(__dirname,`../public/views/images/${new Date().getTime()}.png`)

  fs.writeFile(imgPath, dataBuffer, function(err,ress) {
    if(err){
      res.cc(err)
    }
  });

  let imgpath ='public/views/images/'+path.basename(imgPath)
  const sqlInsertCanvas = 'insert into ev_canvas (name,img) VALUES (?,?)'

  db.query(sqlInsertCanvas,[req_body_name,imgpath],(err,saveCanvasRes)=>{
    if(err){
      res.cc(err)
    }
    canvasId = saveCanvasRes.insertId

    res.cc('保存成功')
  })
}


exports.save_currentEchartsList = (req,res)=>{
  console.log('req.body',req.body)
  // const req_body_name = req.body.fileName
  
  // const req_body_img = req.body.fileUrl
  const req_body_list = req.body.list

  // var dataBuffer = Buffer.from(req_body_img, 'base64');
  // let imgPath = path.join(__dirname,`../public/views/images/${new Date().getTime()}.png`)
  // fs.writeFile(imgPath, dataBuffer, function(err,ress) {
  //   if(err){
  //     res.cc(err)
  //   }
  // });
  // let imgpath ='public/views/images/'+path.basename(imgPath)

  // 定义数据库查询语句
  const sqlStr = 'select * from ev_echart_config where _id = ?'
  req_body_list.forEach((echart)=>{
    db.query(sqlStr,echart.id,(err,echartRes)=>{
      if(err) return res.cc(err)
      if(echartRes.length>0) return res.cc()

      const sqlInsert = 'insert into ev_echart_config set ?'
      const sqlInsertOpt = 'insert into ev_echart_config_option set ?'
      
      db.query(sqlInsert,
        {
          type:echart.type,
          x:echart.x,
          y:echart.y,
          width:echart.width,
          height:echart.height,
          canvas_id:canvasId},(err,configRes)=>{
            if(err) return res.cc(err)
            if(configRes.affectedRows !==1) return res.cc('保存失败')
            db.query(sqlInsertOpt,{
              backgroundColor:echart.option.backgroundColor,
              titile_text:echart.option.title.text,
              xAxis:JSON.stringify(echart.option.xAxis.data),
              color:JSON.stringify(echart.option.color),
              
              yAxis:JSON.stringify(echart.option.yAxis.data),
              _id:configRes.insertId,
            },(err,results)=>{
              if(err){
                return res.cc(err)
              }else if(results.affectedRows !==1){
                return res.cc('保存失败')
              }else{
                res.cc('保存成功')
              }
            })
            
          })
    })

  })

  
}

exports.getCanvesImg=(req,res)=>{
  
}

exports.getEchartCaves=(req,res)=>{
  const req_body_param = req.body

  const sqlGetCaves = 'select * from '

}